/***************************************************************************
 *   Copyright (C) 2006 by Matej Svejda                                    *
 *   mata@aw-modell.at                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "sinusvisualization.h"
#include "sinusvisualization.moc"

static const double pi = 3.14159265;
static const int beatLength = 40;
static const double precision = 0.5;

SinusVisualization::SinusVisualization(QWidget *aTarget, QRect aTargetArea, QObject *parent, const char *name) :
	Visualization(aTarget, aTargetArea, parent, name)
{
	connect(&timer, SIGNAL(timeout()), this, SLOT(draw()));
	
	lineColor = KApplication::palette().active().highlight();
	
	running = false;
}

SinusVisualization::~SinusVisualization()
{

}

void SinusVisualization::setBPM(int aBPM)
{
	bpm = aBPM;
	if(timer.isActive()) {
		start();
	}
}

void SinusVisualization::start()
{
	running = true;
	if(visible) {
		timer.start(static_cast<int>((60000 / bpm) / beatLength));
	}
}

void SinusVisualization::stop()
{
	running = false;
	timer.stop();
}

void SinusVisualization::draw()
{
	static int x = 0;
	static int fakeX = 0;
	static bool faking = false;
	static double verticalExpansionFactor = targetArea.height() / 2;
	static int verticalCenter = targetArea.height() / 2;
	static int maxX = targetArea.width() - 1;
	static double i = 0;
	static int timeout = 0;
	
	if(!faking && x <= maxX) {
		fakeX = x;
	} else if(!faking) {
		faking = true;
		fakeX = maxX;
	}
	
	QPainter painter(target);
	painter.setPen(lineColor);
	
	for(i = 0; i < 1; i += precision) {
		painter.drawPoint(targetArea.left() + fakeX,  targetArea.top() + verticalCenter - static_cast<int>(cos((x + i) * 2 * pi / beatLength) * verticalExpansionFactor));
	}
	
	x++;
	if(faking) {
		bitBlt(target, targetArea.left(), targetArea.top(), target, targetArea.left() + 1, targetArea.top(), fakeX + 1, targetArea.height());
	}
	
	
	if(x >= beatLength && faking && x % beatLength == 0) {
		x = 0;
	}
}


void SinusVisualization::nextBeat(int beatAccentuation)
{

}

void SinusVisualization::setVisibility(bool aVisible)
{
	visible = aVisible;
	if(visible && running) {
		start();
	} else if(!visible && running) {
		stop();
	}
}
